<?php
/**
 * Enter description here...
 *
 * @package if
 * @subpackage module.helper.controllers
 * @since 1.0
 */
class IfHelperApiController extends IHelperController {
	function doIndex() {
		unset($_SERVER["REQUEST_URI"]);
		$func = trim(xn("func"));
		$found = false;
		if ($func) {
			if (function_exists($func)) {
				$reflection = new ReflectionFunction($func);
				if ($reflection->isUserDefined()) {
					$found = true;
					$this->type = "function";
					$this->file = $reflection->getFileName();
					$this->line = $reflection->getStartLine();
					$this->comment = $reflection->getDocComment();
					$this->sign = trim($this->_readline($reflection->getFileName(), $reflection->getStartLine()), "{");
					$this->source = $this->_readMultiLine($reflection->getFileName(), $reflection->getStartLine(), $reflection->getEndLine());
				}
			}
			if (class_exists($func)) {
				$reflection = new ReflectionClass($func);
				if ($reflection->isUserDefined()) {
					$found = true;
					$this->type = "class";
					$this->file = $reflection->getFileName();
					$this->line = $reflection->getStartLine();
					$this->comment = $reflection->getDocComment();
					$this->source = $this->_readMultiLine($reflection->getFileName(), $reflection->getStartLine(), $reflection->getEndLine());
					$this->sign = trim($this->_readline($reflection->getFileName(), $reflection->getStartLine()), "{");
					
					//methods
					$this->methods = array();
					$methods = $reflection->getMethods();
					if (!empty($methods)) {
						foreach ($methods as $method) {
							if (!$method->isPrivate()) {
								$this->methods[] = $method->getDeclaringClass()->getName() . " " . trim($this->_readline($method->getFileName(), $method->getStartLine()), "{");
							}
						}
					}
				}
			}
			if (strstr($func, "::") || strstr($func, "->") || strstr($func, ".")) {
				list($class, $func) = preg_split("/::|\->|\./", $func);
				$class = trim($class);
				$func = trim($func);
				if (method_exists($class, $func)) {
					$this->output("method '{$class}::{$func}' found:", 0);
					$reflection = new ReflectionMethod($class, $func);
					if ($reflection->isUserDefined()) {
						$found = true;
						$this->type = "method";
						$this->file = $reflection->getFileName();
						$this->line = $reflection->getStartLine();
						$this->comment = $reflection->getDocComment();
						$this->source = $this->_readMultiLine($reflection->getFileName(), $reflection->getStartLine(), $reflection->getEndLine());
						$this->sign = trim($this->_readline($reflection->getFileName(), $reflection->getStartLine()), "{");
					}
				}
			}
		}
		$this->similars = $this->_similarFunctions($func);
		$this->found = $found;
		/**
		 * 清空编译文件
		 */
		if_clean_compiler_dir();		
	}
	
	private function _readline($filename, $line) {
		if (!$filename) {
			return;
		}
		$lines = f($filename)->reader()->lines();
		return trim($lines[$line - 1]);
	}
	
	private function _readMultiLine($filename, $start, $end) {
		if (!$filename) {
			return;
		}
		$lines = f($filename)->reader()->lines();
		return implode("", array_slice($lines, $start - 1, $end - $start + 1));
	}
	
	private function _similarFunctions($func) {
		$ret = array();
		$count = 0;
		$funcs = get_defined_functions();
		foreach ($funcs["user"] as $function) {
			if (strtolower($func) == strtolower($function)) {
				continue;
			}
			if (preg_match("/" . preg_quote($func, "/") . "/i", $function)) {
				$ret[] = $function;
				//$this->output("   " . $function);
				$count ++;
				if ($count == 10) {
					break;
				}
			}
		}
		return $ret;
	}
}

?>